//
//  PywCouponHeadView.m
//  PywSdk
//
//  Created by Jade on 16/8/25.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywCouponHeadView.h"
#import "PywMasonry.h"
#import "PywPlatformMain.h"
@interface PywCouponHeadView()
@property (nonatomic,strong) UILabel *promptLabel;
@property (nonatomic,strong) UIView *switchView;
@property (nonatomic,strong) UIView *selectLineView;
@property (nonatomic,strong) NSArray *itemTitles;

@property (nonatomic,strong) NSMutableArray *buttonItems;

@property (nonatomic,assign) NSInteger lastSelectIndex;

@property (nonatomic,assign) CGFloat padding;

@end


@implementation PywCouponHeadView

const CGFloat itemWidth = 80.f;


- (instancetype)initWithItemTitles:(NSArray *)itemTitles

{
    self = [super init];
    if (self) {
        self.itemTitles = itemTitles;
        [self setUp];
    }
    return self;
}

#pragma mark - setUp
- (void)setUp
{
    self.padding = (SCREEN_WIDTH - itemWidth *self.itemTitles.count)/(self.itemTitles.count + 1);
    _buttonItems = [[NSMutableArray alloc] init];
    [self addSubview:self.promptLabel];
    [self addSubview:self.switchView];
    [self.switchView addSubview:self.selectLineView];
    [self setUpSwitchView];
    [self addConstraints];
}

- (void)setUpSwitchView
{
    UIButton *lastButton = nil;
    CGFloat padding = self.padding;
    for (NSInteger  i = 0; i < self.itemTitles.count; i ++) {
        UIButton *button = [self itemButtonWithIndex:i];
        [self.switchView addSubview:button];
        WEAK_SELF;
        [button mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.top.equalTo(weakSelf.switchView.mas_top).offset(5);
            make.bottom.equalTo(weakSelf.switchView.mas_bottom).offset(-5);
            make.width.mas_equalTo(itemWidth);
            if (lastButton) {
                make.left.equalTo(lastButton.mas_right).offset(padding);
            }else {
                make.left.equalTo(weakSelf.mas_left).offset(padding);
            }
        }];
        if (i == 0) {
            button.selected = YES;
            [self.selectLineView mas_makeConstraints:^(PywMASConstraintMaker *make) {
                make.centerX.equalTo(button.mas_centerX);
                make.top.equalTo(button.mas_bottom);
                make.width.equalTo(button.mas_width);
                make.height.mas_equalTo(2);
                
            }];
        }
        lastButton = button;
        [self.buttonItems addObject:button];
    }
    
}


- (void)addConstraints
{
    WEAK_SELF;
    [self.promptLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top);
        make.left.equalTo(weakSelf.mas_left).offset(10);
        make.right.equalTo(weakSelf.mas_right).offset(-10);
        make.height.mas_equalTo(40);
    }];
    
    [self.switchView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.equalTo(weakSelf.promptLabel.mas_bottom);
        make.left.equalTo(weakSelf.mas_left);
        make.right.equalTo(weakSelf.mas_right);
        make.height.mas_equalTo(40.f);
    }];
    
    
}

#pragma mark - getter
- (UILabel *)promptLabel
{
    if (!_promptLabel) {
        _promptLabel = [UILabel new];
        NSString *prompt = [NSString stringWithFormat:@"当前登录，%@",[PywPlatformMain sharedPlatformMain].account];
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:prompt];
        [attributeString addAttribute:NSFontAttributeName
                                value:[UIFont systemFontOfSize:15.f]
                                range:NSMakeRange(0, prompt.length)];
        [attributeString addAttribute:NSForegroundColorAttributeName
                                value:[UIColor blackColor]
                                range:NSMakeRange(0, prompt.length)];
        NSRange range = [prompt rangeOfString:[PywPlatformMain sharedPlatformMain].account];
        if (range.length != NSNotFound) {
            [attributeString addAttribute:NSForegroundColorAttributeName
                                    value:[UIColor orangeColor]
                                    range:range];
        }
        _promptLabel.attributedText = attributeString;
    }
    return _promptLabel;
}

- (UIView *)switchView
{
    if (!_switchView) {
        _switchView = [UIView new];
    }
    return _switchView;
}

- (UIView *)selectLineView
{
    if (!_selectLineView) {
        _selectLineView = [UIView new];
        _selectLineView.backgroundColor = [UIColor orangeColor];
    }
    return _selectLineView;
}

- (UIButton *)itemButtonWithIndex:(NSInteger)index
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = index;
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:[UIColor blackColor]
                 forState:UIControlStateNormal];
    [button setTitleColor:[UIColor orangeColor]
                 forState:UIControlStateSelected];
    [button setTitleColor:[UIColor orangeColor]
                 forState:UIControlStateHighlighted];
    [button setTitle:self.itemTitles[index]
            forState:UIControlStateNormal];
    [button addTarget:self
               action:@selector(selectButton:)
     forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

#pragma mark - Actions
/**
 *  更新item间隔
 *
 */
- (void)updateItemPadding
{
    self.padding = (SCREEN_WIDTH - itemWidth *self.itemTitles.count)/(self.itemTitles.count + 1);
    
    CGFloat padding = self.padding;
    UIButton *lastButton = nil;
    for (UIButton *button in self.buttonItems) {
        WEAK_SELF;
        [button mas_updateConstraints:^(PywMASConstraintMaker *make) {
            if (lastButton) {
                make.left.equalTo(lastButton.mas_right).offset(padding);
            }else {
                make.left.equalTo(weakSelf.mas_left).offset(padding);
            }
        }];
        
        lastButton = button;
    }
}

/**
 *  切换
 *
 *  @param sender <#sender description#>
 */
- (void)selectButton:(UIButton *)sender
{
    NSInteger index = sender.tag;
    if (self.selectItem) {
        self.selectItem(index);
    }
    [self switchItemButtonWithIndex:index];
}
/**
 *  切换选中的item
 *
 *  @param index <#index description#>
 */
- (void)switchItemButtonWithIndex:(NSInteger)index
{
    if (index < 0 || index >= self.buttonItems.count) {
        return;
    }
    
    [self.buttonItems enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button  = (UIButton *)obj;
        if (idx == index) {
            button.selected = YES;
        }else {
            button.selected = NO;
        }
    }];
    
    BOOL isAnimate = YES;
    if (abs(self.lastSelectIndex - index) > 1) {
        isAnimate = NO;
    }
    [self animationLineView:isAnimate index:index];
    self.lastSelectIndex = index;
    
    
}

/**
 *  更新Item title
 *
 *  @param titles <#titles description#>
 */
- (void)updateItemTitle:(NSArray *)titles
{
    for (NSInteger i = 0; i < self.buttonItems.count; i ++) {
        UIButton *button = [self.buttonItems objectAtIndex:i];
        if (i <= titles.count) {
            [button setTitle:titles[i]
                    forState:UIControlStateNormal];
        }
    }
}

/**
 *  底部横线动画
 *
 *  @param isAnimate <#isAnimate description#>
 *  @param index     <#index description#>
 */
- (void)animationLineView:(BOOL)isAnimate
                    index:(NSInteger)index
{
    WEAK_SELF;
    UIButton *button = [self.buttonItems objectAtIndex:index];
    CGFloat duration = isAnimate?.2f:0.f;
    [UIView animateWithDuration:duration
                          delay:0.f
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         CGRect frame = weakSelf.selectLineView.frame;
                         frame.origin.x = button.frame.origin.x;
                         weakSelf.selectLineView.frame = frame;
                     } completion:^(BOOL finished) {
                         
                     }];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/




@end
